import datetime
import re
def reverse_dict(d):
    return dict(sorted(d.items(), key=lambda x: x[1], reverse=True))
def get_name(name,seq=""):
    for i in ["?","、","\\","/","*","\"","\'","|",">","<","\r","\t","\n","=","!","@","#","$","%",":"]:
        name=name.replace(i,seq)
    name=name.strip()
    return name
def get_name_bydata(name,data):
    rst=name
    if isinstance(data,list):
        for i in data:
            rst = rst + "_" + str(i)
    elif isinstance(data,set):
        for i in data:
            rst = rst + "_" + str(i)
    elif isinstance(data,tuple):
        for i in data:
            rst = rst + "_" + str(i)
    elif isinstance(data,dict):
        for k,i in data:
            rst = rst + "_"+str(k) + str(i)
    else:
        rst=rst+"_"+str(data)
    return rst
l=[]
for i in range(10):
    l.append(str(i))
def is_int4str(i):
    if i in l:
        return True
    else:
        return False
def get_int4str(s):
    r=""
    for i in s:
        if is_int4str(i):
            r=r+i
    return r
numberlist=["0","1","2","3","4","5","6","7","8","9"]
def isnumber(i):
    if str(i) in numberlist:
        return True
    else:
        return False
def getint_bystr(a):
    n=""
    for i in a:
        if isnumber(i):
            n=n+i
        elif i=="-":
            n=n+i
    return int(n)
def getfloat_bystr(a):
    n = ""
    for i in a:
        if isnumber(i):
            n = n + i
        elif i in [".","-"]:
            n=n+i
    return float(n)

def sortdict(a,ascending=False,by="value"):
    l=[]
    rst = {}
    if by=="value":
        # try:
        for k, v in a.items():
            # if v:
            l.append((v,k))
        if ascending:
            l = sorted(l, reverse=False)
        else:
            l=sorted(l,reverse=True)
        for i in l:
            k=i[1]
            rst[k]=i[0]
        # except Exception as e:
        #     print("sortdict err:",l,e)
    else:
        for k, v in a.items():
            l.append((k,v))
        if ascending:
            l = sorted(l, reverse=False)
        else:
            l=sorted(l,reverse=True)
        for i in l:
            k=i[0]
            rst[k]=i[1]
    return rst
def sort_dictlist(a,ascending=False,by="value"):
    if ascending:
        a0 = sorted(a, key=lambda x: x[by])
    else:
        a0 = sorted(a, key=lambda x: x[by],reverse=True)
    return a0
def dict2str(d):
    rst=""
    for k,v in d.items():
        if isinstance(v,float):
            vv=str(round(v,2))
        elif isinstance(v,datetime.datetime):
            vv=str(v)[:10]
        else:
            vv=str(v)
        if rst=="":
            rst=rst+k+":"+vv
        else:
            rst = rst+","+k+":"+vv
    return rst
def deepdict2dict(r,k=None):
    rst={}
    for k1,v1 in r.items():
        k1=str(k1)
        if k:
            key=k+"_"+k1
        else:
            key=k1
        if isinstance(v1, dict):
            rst.update(deepdict2dict(v1,key))
        elif isinstance(v1,list):
            n=1
            for i in v1:
                key0=key+str(n)
                n=n+1
                rst[key0] =i
        else:
            rst[key]=v1
    return rst
def get_createtimekey(url):
    import os
    import datetime
    t = os.path.getctime(url)
    t=datetime.datetime.fromtimestamp(t)
    return t
def is_path(string):
    pattern = r'^[a-zA-Z]:[/\\].*$'  # Windows路径的正则表达式
    # pattern = r'^/.*$'  # Linux路径的正则表达式
    return re.match(pattern, string) is not None
if __name__=="__main__":
    a={"a":1,"b":0}
    print(sortdict(a))